package cn.sdormitory.controller.knowledgebase;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.knowledgebase.pojo.StudyQuestions;
import cn.sdormitory.knowledgebase.pojo.StudySkills;
import cn.sdormitory.knowledgebase.pojo.StudySkillsBatch;
import cn.sdormitory.knowledgebase.service.StudySkillsService;
import cn.sdormitory.knowledgebase.vo.RelatedUserVo;
import cn.sdormitory.knowledgebase.vo.SkillBatchVO;
import cn.sdormitory.knowledgebase.vo.StudySkillsVo;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.tencent.service.TencentCosFileService;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "技能学习")
@RequestMapping("/studySkills")
public class StudySkillsController {
    @Resource
    private StudySkillsService studySkillsService;
    @Resource
    private TencentCosFileService tencentCosFileService;

    @ApiOperation("技能列表-分页查询")
    @GetMapping("/getList")
    public CommonResult<CommonPage<StudySkillsVo>> getListByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "type", required = false) Integer type
    ) {
        CommonPage<StudySkillsVo> studySkillsIPage = studySkillsService.getStudySkillsList(title, type, pageNum, pageSize);
        return CommonResult.success(studySkillsIPage);
    }
    @ApiOperation("技能列表-批量删除")
    @DeleteMapping(value = "/delSkills/{ids}")
    public CommonResult delSkills(@PathVariable Integer[] ids) {
        studySkillsService.delSkills(ids);
        return CommonResult.success();
    }
    @ApiOperation(value = "技能列表-获取技能对应批次信息")
    @GetMapping("/getSkillsBatchList/{id}")
    public CommonResult<List<SkillBatchVO>> getSkillsBatchList(@PathVariable("id") Integer skillId) {
        List<SkillBatchVO> list = studySkillsService.getSkillsBatchList(skillId);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "上传技能封面图片到腾讯云oos")
    @PostMapping("/uploadSkillsPhoto")
    public CommonResult<String> uploadSkillsPhoto(@RequestParam(value = "file") MultipartFile file) {
        String photo = tencentCosFileService.uploadImg(file);
        Assert.notNull(photo,"上传技能封面图片到腾讯云失败");
        return CommonResult.success(photo);
    }
    @ApiOperation(value = "上传技能视频到腾讯云oos")
    @PostMapping("/uploadSkillsVideo")
    public CommonResult<String> uploadSkillsVideo(@RequestParam(value = "file") MultipartFile file) {
        String video = tencentCosFileService.uploadVideo(file);
        Assert.notNull(video,"上传技能视频到腾讯云失败");
        return CommonResult.success(video);
    }

    @ApiOperation(value = "技能编辑列表-根据id获取技能学习内容")
    @GetMapping("/getStudySkillsById/{id}")
    public CommonResult<StudySkills> getStudySkillsById(@PathVariable("id") Integer id) {
        return CommonResult.success(studySkillsService.getStudySkillsById(id));
    }
    @ApiOperation(value = "技能编辑列表-新增修改技能")
    @PostMapping("/editSkills")
    public CommonResult editSkills(@RequestBody StudySkills studySkills) {
        studySkillsService.editSkills(studySkills);
        return CommonResult.success();
    }


    @ApiOperation(value = "技能关联试题-查询已关联试题")
    @GetMapping("/getRelatedQuestionsList")
    public CommonResult<CommonPage<StudyQuestions>> getRelatedQuestionsList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "skillsId") Integer skillsId,
            @RequestParam(value = "category",required = false) Integer category) {
        CommonPage<StudyQuestions> list = studySkillsService.getRelatedQuestionsList(skillsId,category, pageNum, pageSize);
        return CommonResult.success(list);
    }
    @ApiOperation(value = "技能关联试题-查询未关联试题")
    @GetMapping("/getUnRelatedQuestionsList")
    public CommonResult<CommonPage<StudyQuestions>> getUnRelatedQuestionsList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "skillsId") Integer skillsId,
            @RequestParam(value = "category",required = false) Integer category) {
        CommonPage<StudyQuestions> list = studySkillsService.getUnRelatedQuestionsList(skillsId,category,pageNum, pageSize);
        return CommonResult.success(list);
    }
    @ApiOperation(value = "技能关联试题-关联试题")
    @PostMapping("/relatedQuestions/{id}")
    public CommonResult relatedQuestions(@PathVariable("id") Integer skillId, @RequestBody Integer[] questionIds) {
        studySkillsService.relatedQuestions(skillId,questionIds);
        return CommonResult.success();
    }
    @ApiOperation(value = "技能关联试题-解绑试题")
    @PostMapping("/unRelatedQuestions/{id}")
    public CommonResult unRelatedQuestions(@PathVariable("id") Integer skillId, @RequestBody Integer[] questionIds){
        studySkillsService.unRelatedQuestions(skillId,questionIds);
        return CommonResult.success();
    }

    @ApiOperation(value = "技能批次关联用户-查询已关联用户")
    @GetMapping("/getRelatedUserList")
    public CommonResult<List<RelatedUserVo>> getRelatedUserList(
            @RequestParam(value = "batchId") Integer batchId,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "deptId", required = false) Integer deptId) {
        List<RelatedUserVo> list = studySkillsService.getRelatedUserList(batchId,userName,deptId);
        return CommonResult.success(list);
    }
    @ApiOperation(value = "技能批次关联用户-查询未关联用户")
    @GetMapping("/getUnRelatedUserList")
    public CommonResult<List<RelatedUserVo>> getUnRelatedUserList(
            @RequestParam(value = "batchId") Integer batchId,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "deptId", required = false) Integer deptId) {
        List<RelatedUserVo> list = studySkillsService.getUnRelatedUserList(batchId,userName,deptId);
        return CommonResult.success(list);
    }
    @ApiOperation(value = "技能批次关联用户-关联用户")
    @PostMapping("/relatedUsers/{id}")
    public CommonResult relatedUsers(@PathVariable("id") Integer batchId, @RequestBody Integer[] userIds) {
        studySkillsService.relatedUsers(batchId,userIds);
        return CommonResult.success();
    }
    @ApiOperation(value = "技能批次关联用户-解绑用户")
    @PostMapping("/unRelatedUsers/{id}")
    public CommonResult unRelatedUsers(@PathVariable("id") Integer batchId, @RequestBody Integer[] userIds){
        studySkillsService.unRelatedUsers(batchId,userIds);
        return CommonResult.success();
    }


    @ApiOperation(value = "技能批次编辑列表-获取技能下最后一批次的信息")
    @GetMapping("/getLastBatchBySkillsId/{id}")
    public CommonResult<StudySkillsBatch> getLastBatchBySkillsId(@PathVariable("id") Integer skillId) {
        StudySkillsBatch batch = studySkillsService.getLastBatchBySkillsId(skillId);
        return CommonResult.success(batch);
    }
    @ApiOperation(value = "技能批次编辑列表-获取指定批次信息")
    @GetMapping("/getBatchById/{id}")
    public CommonResult<StudySkillsBatch> getBatchById(@PathVariable("id") Integer batchId) {
        StudySkillsBatch batch = studySkillsService.getBatchById(batchId);
        return CommonResult.success(batch);
    }
    @ApiOperation(value = "技能批次编辑列表-新增修改技能批次")
    @PostMapping("/editBatch")
    public CommonResult editBatch(@RequestBody StudySkillsBatch batch) {
        studySkillsService.editBatch(batch);
        return CommonResult.success();
    }
    @ApiOperation("技能批次编辑列表-删除批次")
    @DeleteMapping(value = "/delBatch/{id}")
    public CommonResult delBatch(@PathVariable("id") Integer id) {
        studySkillsService.delBatch(id);
        return CommonResult.success();
    }
    @ApiOperation("技能批次编辑列表-启用批次")
    @GetMapping("/openBatch")
    public CommonResult openBatch(@RequestParam("batchId") Integer batchId,
                                  @RequestParam("skillsId") Integer skillsId) {
        studySkillsService.openBatch(batchId,skillsId);
        return CommonResult.success();
    }
    @ApiOperation("技能批次编辑列表-关闭批次")
    @GetMapping("/closeBatch/{id}")
    public CommonResult closeBatch(@PathVariable("id") Integer batchId) {
        studySkillsService.closeBatch(batchId);
        return CommonResult.success();
    }
}
